package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.domain.QuestionItem;
import cn.wolfcode.qo.MyAJAX;
import cn.wolfcode.service.IExamService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IQuestionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private IExamService examService;
    @Autowired
    private IQuestionService questionService;


    @RequiredPermission(name="试卷页面",expression="exam:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Exam> pageInfo = examService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "exam/list";
    }


    @RequiredPermission(name="试卷删除",expression="exam:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           examService.delete(id);
        }
        return "redirect:/exam/list";
    }



    @RequestMapping("/新增试卷页面")
    public String input(Exam exam) {
        return "exam/新增试卷页面";
    }


    @RequiredPermission(name="试卷新增/编辑",expression="exam:saveOrUpdate")
    @RequestMapping("/insert")
    @ResponseBody
    public MyAJAX save(Exam exam) {
        //生成试卷
        examService.createExam(exam);
        MyAJAX myAJAX = new MyAJAX();
        myAJAX.setId(exam.getId());
        return myAJAX;
    }

    @RequestMapping("/试卷预览页面")
    public String showPage(Long id,Model model){
        //单选分数
        int oneScore = 0;
        //多选分数
        int manyScore = 0;
        //判断分数
        int ifScore = 0;
        Exam exam = examService.get(id);
        List<Question> oneList = examService.selectQuestionList(exam.getId(), 1);
        if(oneList!= null) {
            for (Question question : oneList) {
                //根据题目id查询出所有选项
                question.setOptions(questionService.selectByQuestionId(question.getId()));
                oneScore += question.getScore();
                //选项答案
                List<QuestionItem> options = question.getOptions();
                for (QuestionItem Item : options) {
                    if (Item.isJudge_right()){
                        String[] split = Item.getTitle().split("\\.");
                        question.getRight().append(split[0]);
                    }
                }
            }
            exam.setOneScore(oneScore);
        }
        exam.setOneList(oneList);
        List<Question> manyList = examService.selectQuestionList(exam.getId(), 2);
        if(manyList!= null) {
            for (Question question : manyList) {
                //根据题目id查询出所有选项
                manyScore += question.getScore();
                question.setOptions(questionService.selectByQuestionId(question.getId()));
                List<QuestionItem> options = question.getOptions();
                for (QuestionItem Item : options) {
                    if (Item.isJudge_right()){
                        String[] split = Item.getTitle().split("\\.");
                        question.getRight().append(split[0]);
                    }
                }
            }
            exam.setManyScore(manyScore);
        }
        exam.setManyList(manyList);
        List<Question> ifList = examService.selectQuestionList(exam.getId(), 3);
        if(manyList!= null) {
            for (Question question : ifList) {
                //根据判断题特殊处理
                ifScore += question.getScore();
                question.setIfQuestion(questionService.selectByIFQuestion(exam.getId(), question.getId()));
            }
            exam.setIfScore(ifScore);
        }
        exam.setIfList(ifList);
        model.addAttribute("Exam",exam);
        return "exam/试卷预览页面";
    }
}
